<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */
// ------------------------------------------------------------------------

/**
 * XML-RPC server class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	XML-RPC
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/xmlrpc.html
 */
class MY_Xmlrpcs extends CI_Xmlrpcs {
    // --------------------------------------------------------------------

    /**
     * Executes the Method
     *
     * @access	protected
     * @param	object
     * @return	mixed
     */
    function _execute($m) {
        $methName = $m->method_name;

        // Check to see if it is a system call
        $system_call = (strncmp($methName, 'system', 5) == 0) ? TRUE : FALSE;

        if ($this->xss_clean == FALSE) {
            $m->xss_clean = FALSE;
        }

        //-------------------------------------
        //  Valid Method
        //-------------------------------------

        if (!isset($this->methods[$methName]['function'])) {
            return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
        }

        //-------------------------------------
        //  Check for Method (and Object)
        //-------------------------------------

        $method_parts = explode(".", $this->methods[$methName]['function']);
        $objectCall = (isset($method_parts['1']) && $method_parts['1'] != "") ? TRUE : FALSE;

        if ($system_call === TRUE) {
            if (!is_callable(array($this, $method_parts['1']))) {
                return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
            }
        } else {
            $CI = & get_instance();
            $metodo = strtolower($method_parts['0']);
            $CI->load->library($metodo);

            if ($objectCall && !is_callable(array($method_parts['0'], $method_parts['1']))) {
                return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
            } elseif (!$objectCall && !is_callable($this->methods[$methName]['function'])) {
                return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
            }
        }

        //-------------------------------------
        //  Checking Methods Signature
        //-------------------------------------

        if (isset($this->methods[$methName]['signature'])) {
            $sig = $this->methods[$methName]['signature'];
            for ($i = 0; $i < count($sig); $i++) {
                $current_sig = $sig[$i];

                if (count($current_sig) == count($m->params) + 1) {
                    for ($n = 0; $n < count($m->params); $n++) {
                        $p = $m->params[$n];
                        $pt = ($p->kindOf() == 'scalar') ? $p->scalarval() : $p->kindOf();

                        if ($pt != $current_sig[$n + 1]) {
                            $pno = $n + 1;
                            $wanted = $current_sig[$n + 1];

                            return new XML_RPC_Response(0,
                                    $this->xmlrpcerr['incorrect_params'],
                                    $this->xmlrpcstr['incorrect_params'] .
                                    ": Wanted {$wanted}, got {$pt} at param {$pno})");
                        }
                    }
                }
            }
        }

        //-------------------------------------
        //  Calls the Function
        //-------------------------------------

        if ($objectCall === TRUE) {
            if ($method_parts[0] == "this" && $system_call == TRUE) {
                return call_user_func(array($this, $method_parts[1]), $m);
            } else {
                if ($this->object === FALSE) {
                    return $CI->$metodo->$method_parts['1']($m);
                } else {
                    return $this->object->$method_parts['1']($m);
                    //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
                }
            }
        } else {
            return call_user_func($this->methods[$methName]['function'], $m);
        }
    }

}

// END XML_RPC_Server class


/* End of file Xmlrpcs.php */
/* Location: ./system/libraries/Xmlrpcs.php */